<?php
namespace app\backstage\controller;
use app\reception\logic\LogLogic;
use think\Controller;
use think\Db;
use think\facade\Request;

class Monitoring extends Controller
{
    /*平台下发监测链接数据接收*/
    public function receive()
    {
        $params = Request::param('', '', 'strip_tags');
        if( !( isset($params['adtype']) && strlen($params['adtype']) >= 1 ) )
        {
            return false;
        }

        switch ($params['adtype'])
        {
            case 'jl':
                $result = $this->jl_receive($params);
                break;
            case 'ks':
                $result = $this->ks_receive($params);
                break;
            case 'baidu':
                $result = $this->baidu_receive($params);
                break;
            case 'uc':
                $result = $this->uc_receive($params);
                break;
            case 'vivo':
                $result = $this->vivo_receive($params);
                break;
            case 'oppo':
                $result = $this->oppo_receive($params);
                break;
            case 'bilibili':
                $result = $this->bilibili_receive($params);
                break;
            default:
                $result = $this->jl_receive($params);
        }

        return $result;
    }

    /*快手监测链接监测数据*/
    public function ks_receive($param)
    {

    }

    /*巨量二跳接收监测数据*/
    private function jl_receive($param)
    {
//        LogLogic::addLog('2t',json_encode($param,JSON_UNESCAPED_UNICODE));
        if (!isset($param['channel_id']) || !isset($param['callback']) || $param['callback'] == '__CALLBACK_PARAM__') {
            $res['ret'] = 500;
            $res['msg'] = 'test jl-ad requests';
            return json($res);
        }
        if (!$param['request_id']) {
            $res['ret'] = 500;
            $res['msg'] = 'have jl-ad requests';
            return json($res);
        }
        $cid = '';
        /*if(isset($param['mid1']) && $param['mid1'] != '__MID1__')
        {
            $cid = $param['mid1'];
        }
        if(isset($param['mid2']) && $param['mid2'] != '__MID2__')
        {
            $cid = $param['mid2'];
        }*/
        if (isset($param['mid3']) && $param['mid3'] != '__MID3__') {
            $cid = $param['mid3'];
        }
        /*   if(isset($param['mid4']) && $param['mid4'] != '__MID4__')
           {
               $cid = $param['mid4'];
           }
           if(isset($param['mid5']) && $param['mid5'] != '__MID5__')
           {
               $cid = $param['mid5'];
           }
           if(isset($param['mid6']) && $param['mid6'] != '__MID6__')
           {
               $cid = $param['mid6'];
           }*/

        $oaid = '';
        if (isset($param['oaid']) && $param['oaid'] && $param['oaid'] != '__OAID__') {
            $oaid = $param['oaid'];
        }
        if (isset($param['oaidmd5']) && $param['oaidmd5'] && $param['oaidmd5'] != '__OAID_MD5__') {
            $oaidmd5 = $param['oaidmd5'];
        } else {
            $oaidmd5 = strlen($oaid) > 1 ? md5($param['oaid']) : '';
        }

        $usersign = md5(get_phone_brand($param['ua']).'_'.$param['ip']);

        $data = [];
        $data['ip_addr']        = $param['ip'];
        $data['link']           = json_encode($param, JSON_UNESCAPED_UNICODE);
        $data['created_at']     = date('Y-m-d H:i:s');
        $data['channel_id']     = $param['channel_id'];
        $data['type']           = 3;
        $data['usersign']       = $usersign;
        $data['advertiser_id']  = (isset($param['adid']) && $param['adid']) ? $param['adid'] : '';
        //1.0广告组ID 2.0项目ID
        $data['campaign_id']    = (isset($param['campaign_id']) && $param['campaign_id']) ? $param['campaign_id'] : '';
        $data['aid']            = (isset($param['aid']) && $param['aid']) ? $param['aid'] : '';
        $data['cid']            = $cid; //2.0素材ID
        //$data['cid']          = (isset($param['cid']) && $param['cid']) ? $param['cid'] : ''; //1.0创意ID
        $data['oaid_md5']       = $oaidmd5;

        Db::table('quick_user_behavior_monitoring')->insertGetId($data);

        $res['ret'] = 0;
        $res['msg'] = 'success';
        return json($res);
    }

    /**
     * https://monitor.yfwlkjqs.cn/monitoring_receive?adtype=baidu&channel_id=361&ip=__IP__&ua=__UA__&ts=__TS__&userid=__USER_ID__&pid=__PLAN_ID__
     * &aid=__IDEA_ID__&click_id=__CLICK_ID__&callback_url=__CALLBACK_URL__&bd_vid=__BD_VID__&oaidmd5=__OAID_MD5__&mysign=av8Jn39f*
     */
    /*百度接收监测数据*/
    private function baidu_receive($param){

        if(!isset($param['callback_url']) || $param['callback_url'] == '__CALLBACK_URL__') {
            $res['ret'] = 500;
            $res['msg'] = 'test baidu-ad requests';
            return json($res);
        }

        $usersign = md5(get_phone_brand($param['ua']).'_'.$param['ip']);

        $data = [];
        $data['ip_addr']        = $param['ip'];
        $data['link']           = json_encode($param, JSON_UNESCAPED_UNICODE);
        $data['created_at']     = date('Y-m-d H:i:s');
        $data['channel_id']     = $param['channel_id'];
        $data['type']           = 4;
        $data['usersign']       = $usersign;
        $data['adsign']         = $param['mysign'];
        $data['advertiser_id']  = $param['userid'] && $param['userid'] != "NULL" ? $param['userid'] : '';//广告主id
        $data['campaign_id']    = '';//广告组id
        $data['aid']            = $param['pid'] && $param['pid'] != "NULL" ? $param['pid'] : '';//广告计划id
        $data['cid']            = $param['aid'] && $param['aid'] != "NULL" ? $param['aid'] : '';//广告创意id
        $data['oaid_md5']       = $param['oaidmd5'];

        Db::table('quick_user_behavior_monitoring')->insertGetId($data);
    }

    /**
     * https://monitor.yfwlkjqs.cn/monitoring_receive?adtype=uc&channel_id=361
     * &imei_sum={IMEI_SUM}&imei_sum1={IMEI_SUM1}&oaid={OAID}&oaidmd5={OAID_SUM}&ts={TS}
     * &callback={CALLBACK}&ip={IP}&aid={AID}&cid={CID}&ua={UA}&advertiser_id={ACID}&campaign_id={GID}
     */
    /*uc接收监测数据*/
    private function uc_receive($param){

        if(!isset($param['callback']) || $param['callback'] == '{CALLBACK_URL}' || $param['callback'] == '{CALLBACK}') {
            $res['ret'] = 500;
            $res['msg'] = 'test uc-ad requests';
            return json($res);
        }

        $usersign = md5(get_phone_brand($param['ua']).'_'.$param['ip']);

        $data = [];
        $data['ip_addr']        = $param['ip'];
        $data['link']           = $param['callback'];
        $data['created_at']     = date('Y-m-d H:i:s');
        $data['channel_id']     = $param['channel_id'];
        $data['type']           = 5;
        $data['usersign']       = $usersign;
        $data['adsign']         = '';
        $data['advertiser_id']  = $param['advertiser_id'] ? $param['advertiser_id'] : '';//广告主id
        $data['campaign_id']    = $param['campaign_id'] ? $param['campaign_id'] : '';//广告组id
        $data['aid']            = $param['aid'] ? $param['aid'] : '';//广告计划id
        $data['cid']            = $param['cid'] ? $param['cid'] : '';//广告创意id
        $data['oaid_md5']       = $param['oaidmd5'] ? $param['oaidmd5'] : '';

        Db::table('quick_user_behavior_monitoring')->insertGetId($data);
    }

    /**
     * https://monitor.yfwlkjqs.cn/monitoring_receive?adtype=vivo&channel_id=361&creativeId=__CREATIVEID__&reqId=__REQUESTID__
     * &oaid=__OAID__&oaidmd5=__OAIDMD5__&mysign=lkh5qGlf
    */
    /*vivo接收监测数据*/
    private function vivo_receive($param)
    {
//        LogLogic::addLog('vivo', json_encode($param));
//        if (!isset($param['channel_id']) || $param['channel_id'] == '' || !isset($param['mysign']) || $param['mysign'] == '') {
//            $res['ret'] = 500;
//            $res['msg'] = 'test vivo-ad requests';
//            return json($res);
//        }
//        $ip = request()->ip();
//        $usersign = $param['reqId'];
//        $akey = Db::table("quick_ad_account_vivo")->where("mysign", $param['mysign'])->cache(3600)->value('akey');//数据源id
//        $data = [];
//        $data['ip_addr'] = $ip;
//        $data['link'] = json_encode($param, JSON_UNESCAPED_UNICODE);
//        $data['created_at'] = date('Y-m-d H:i:s');
//        $data['channel_id'] = $param['channel_id'];
//        $data['type'] = 6;
//        $data['usersign'] = $usersign;
//        $data['adsign'] = $param['mysign'];
//        $data['advertiser_id'] = $akey ? $akey : '';//广告主id
//        $data['campaign_id'] = '';//广告组id
//        $data['aid'] = '';//广告计划id
//        $data['cid'] = '';//广告创意id
//        $data['oaid_md5'] = $param['oaidmd5'] ? $param['oaidmd5'] : '';
//        Db::table('quick_user_behavior_monitoring')->insertGetId($data);
    }

    /**
     * https://monitor.yfwlkjqs.cn/monitoring_receive?adtype=oppo&channel_id=361
     * &ip=__IP__&imei=$im$&oaid=__OAID__&req_id=$req$&advertiser_id=$ownerid$&campaign_id=$groupid$&aid=$planid$&cid=$ad$
     */
    /*oppo接收监测数据*/
    private function oppo_receive($param)
    {

        if (!isset($param['channel_id']) || $param['channel_id'] == '' || !isset($param['req_id']) || $param['req_id'] == '$req$') {
            $res['ret'] = 500;
            $res['msg'] = 'test oppo-ad requests';
            return json($res);
        }

        $usersign = md5(get_phone_brand($param['ua']).'_'.$param['ip']);

        $data = [];
        $data['ip_addr']        = $param['ip'];
        $data['link']           = json_encode($param, JSON_UNESCAPED_UNICODE);
        $data['created_at']     = date('Y-m-d H:i:s');
        $data['channel_id']     = $param['channel_id'];
        $data['type']           = 7;
        $data['usersign']       = $usersign;
        $data['adsign']         = '';
        $data['advertiser_id']  = $param['advertiser_id'] ? $param['advertiser_id'] : '';//广告主id
        $data['campaign_id']    = $param['campaign_id'] ? $param['campaign_id'] : '';//广告组id
        $data['aid']            = $param['aid'] ? $param['aid'] : '';//广告计划id
        $data['cid']            = $param['cid'] ? $param['cid'] : '';//广告创意id
        $data['oaid_md5']       = $param['oaid'] && $param['oaid'] != '__OAID__' ? md5($param['oaid']) : '';

        Db::table('quick_user_behavior_monitoring')->insertGetId($data);
    }


    /**
     * b站监测链接
     * @param $param
     * @return \think\response\Json
     */
    public function bilibili_receive($param){
        //LogLogic::addLog('bilibili', json_encode($param));

        if(!isset($param['advertiser_id']) || $param['advertiser_id'] == '__ACCOUNTID__') {
            $res['ret'] = 500;
            $res['msg'] = 'test bilibili-ad requests';
            return json($res);
        }

        $usersign = md5(get_phone_brand($param['ua']).'_'.$param['ip']);

        $data = [];
        $data['ip_addr']        = $param['ip'];
        $data['link']           = json_encode($param, JSON_UNESCAPED_UNICODE);;
        $data['created_at']     = date('Y-m-d H:i:s');
        $data['channel_id']     = $param['channel_id'];
        $data['type']           = 5;
        $data['usersign']       = $usersign;
        $data['adsign']         = '';
        $data['advertiser_id']  = $param['advertiser_id'];//广告主id
        $data['campaign_id']    = $param['campaign_id'];//广告组id
        $data['aid']            = $param['aid'];//广告计划id
        $data['cid']            = $param['cid'];//广告创意id
        $data['oaid_md5']       = $param['oaidmd5'];

        Db::table('quick_user_behavior_monitoring')->insertGetId($data);
    }
}